﻿using System;
using System.IO;
using System.Web.Mvc;
using KLS.PPDS.Utilities;
using KLS.PPDS.ApplicationService.Manager;

namespace KLS.PPDS.Web.Controllers.Article
{
    public class GenericFileActionResult : ActionResult
    {
        byte[] FileStream { get; set; }
        public string FullFileName { get; set; }
   
        public override void ExecuteResult(ControllerContext context)
        {
            if (String.IsNullOrEmpty(FullFileName).Equals(true))
            {
                throw new ArgumentNullException("FullFileName");
            }

            FileStream = FileUtil.GetFile(this.FullFileName);

            context.HttpContext.Response.Clear();
            context.HttpContext.Response.ContentType = UploadFileTypeManager.GetMimeTypeFromExtension(this.FullFileName);
            context.HttpContext.Response.OutputStream.Write(FileStream, 0, FileStream.Length);

            context.HttpContext.Response.AppendHeader("Content-Disposition", "filename=\""+Path.GetFileName(FullFileName)+"\"");

            context.HttpContext.Response.Flush();
            try { context.HttpContext.Response.End(); }
            catch { }

        }   
    }
}